package workers;

import java.util.concurrent.BlockingQueue;

import workerUIs.StepFrame;
import workerUIs.TestFrame;
import model.Software;

public class TestStepWorker extends AbstractStepSwingWorker {

	private final BlockingQueue<Software> inputQueue;
	private final BlockingQueue<Software> outputRepair;
	private final BlockingQueue<Software> outputRelease;
	private StepFrame nextFrameRepair;
	private StepFrame nextFrameRelease;

	public TestStepWorker(StepFrame frame, BlockingQueue<Software> inputQueue, BlockingQueue<Software> outputRepair, BlockingQueue<Software> outputRelease, StepFrame repairFrame, StepFrame releaseFrame) {
		super(frame);
		this.inputQueue = inputQueue;
		this.outputRepair = outputRepair;
		this.outputRelease = outputRelease;
		this.nextFrameRepair = repairFrame;
		this.nextFrameRelease = releaseFrame;
	}

	@Override
	protected Software doInBackground() throws Exception {
		this.software = this.inputQueue.take();
		if (getFrame() instanceof TestFrame) {
			if (((TestFrame)getFrame()).isTestSuccessful()) {
				this.outputRelease.put(this.software);
			} else {
				this.outputRepair.put(this.software);
			}
		}
		return this.software;
	}
	
	@Override
	protected void done() {
		super.done();
		if (inputQueue.isEmpty()) {
			getFrame().getNextButton().setEnabled(false);
		}
		if (((TestFrame)getFrame()).isTestSuccessful()) {
			this.nextFrameRelease.getNextButton().setEnabled(true);	
		} else {
			this.nextFrameRepair.getNextButton().setEnabled(true);
		}
	}

}
